import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;


public class PostProc {
	
	private int inputFileNum; 
	private ArrayList<Integer> sensorNumsList = new ArrayList<Integer>();
	private ArrayList<double[]> ratesList = new ArrayList<double[]>();
	private ArrayList<Double> meansList = new ArrayList<Double>();
	private ArrayList<Double> deviationList = new ArrayList<Double>();
	
	public static void main(String[] args) {
		
		PostProc p = new PostProc();
		p.proc();
//		double[] sample = {1.2, 2.8, 2.0, 2.0};
//		double mean = p.calMean(sample);
//		double deviation = p.calDeviation(sample, mean);
//		System.out.println(mean + "; " + deviation);
	}

	/**
	 * @param p
	 */
	private void proc() {
		File dir = new File(".");
		File[] files = dir.listFiles(new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String name) {
				if (name.contains("res") && name.contains(".txt")) {
					return true;
				} else {
					return false;
				}
			}
		});
		//System.out.println(dir.getAbsolutePath());
		//System.out.println(files.length);
		inputFileNum = files.length;
		try {
			for (int i = 0; i < inputFileNum; i++) {
				readFile(files[i], i);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < ratesList.size(); i++) {
//			for (double  d : ratesList.get(i)) {
//				
//				System.out.println(d);
//			}
			double mean = calMean(ratesList.get(i));
			meansList.add(mean);
			double deviation = calDeviation(ratesList.get(i), mean);
			deviationList.add(deviation);
			System.out.format("%5d %.4f %.4f\n", sensorNumsList.get(i), mean, deviation);
		}
	}


	private double calMean(double[] scores) {
		double sum = 0;;
		for (double d : scores) {
			sum += d;
		}
		
		return sum / scores.length;
	}
	
	private double calDeviation(double[] scores, double mean) {
		double sum = 0;
		for (double d : scores) {
			sum += (d - mean) * (d - mean);
		}
		return Math.sqrt(sum / scores.length);
	}

	/**
	 * @param path
	 * @throws FileNotFoundException
	 */
	private void readFile(File file, int fileNo) throws FileNotFoundException {
		BufferedReader br = null;
		br = new BufferedReader(new FileReader(file));
		String line = null;
		int iteration = 0;
		try {
			while ((line = br.readLine()) != null) {
				if (line.contains("TestNum")) {
					line = br.readLine();
					String[] split = line.trim().split("\\s+");
					// First file, add arrays
					if (fileNo == 0) {
						sensorNumsList.add(Integer.parseInt(split[0]));
						ratesList.add(new double[inputFileNum]);
					}
					if (sensorNumsList.get(iteration) != Integer
							.parseInt(split[0])) {
						throw new RuntimeException("Not consistant data files");
					}
					String rate = split[1].replace(',', '.');
					ratesList.get(iteration)[fileNo] = Double
							.parseDouble(rate);
					iteration++;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
